<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
          "http://www.w3.org/TR/html4/loose.dtd">

<html lang="en">

<head>
  <meta http-equiv="Content-Type"
        content="text/html; charset=utf-8">
  <meta http-equiv="Content-Style-Type"
        content="text/css">
  <meta http-equiv="Content-Script-Type"
        content="text/javascript">
  <meta name="description"
        content="FreeType Documentation">
  <meta name="Author"
        content="David Turner">

  <link rel="icon"
        href="../image/favicon_-90.ico">
  <link rel="shortcut icon"
        href="../image/favicon_-90.ico">
  <link rel="stylesheet"
        type="text/css"
        href="../css/freetype2_-90.css">

  <script type="text/javascript"
          src="../../../js/jquery-1.11.0.min.js">
  </script>
  <script type="text/javascript"
          src="../../../js/jquery.ba-resize.min.js">
  </script>
  <script type="text/javascript"
          src="../../../js/freetype2.js">
  </script>

  <title>FreeType Glyph Conventions / VII</title>
</head>


<body>

<div id="top"
     class="bar">
  <h1><a href="http://freetype.org/index.html">FreeType</a> Glyph
    Conventions&nbsp;/&nbsp;VII</h1>
</div>


<div id="wrapper">

<div class="colmask leftmenu">
  <div class="colright">
    <div class="col1wrap">
      <div class="col1">


        <!-- ************************************************** -->

        <div id="freetype-bitmaps">
	  <h2>VII. FreeType Bitmaps</h2>

	  <p>The purpose of this section is to present the way
	    FreeType manages bitmaps and pixmaps, and how they relate
	    to the concepts previously defined.  The relationship
	    between vectorial and pixel coordinates is explained.</p>


	  <h3 id="section-1">1. Vectorial versus pixel
	    coordinates</h3>

	  <p>This sub-section explains the difference between
	    vectorial and pixel coordinates.  To make things clear,
	    brackets will be used to describe pixel coordinates,
	    e.g. &lsquo;[3,5]&rsquo;, while parentheses will be used
	    for vectorial ones, e.g. &lsquo;(-2,&nbsp;3.5)&rsquo;.</p>

	  <p>In the pixel case, as we use the <em>Y&nbsp;upwards</em>
	    convention; the coordinate [0,&nbsp;0] always refers to
	    the <em>lower left pixel</em> of a bitmap, while
	    coordinate [width-1,&nbsp;rows-1] to its <em>upper right
	    pixel</em>.</p>

	  <p>In the vectorial case, point coordinates are expressed in
	    floating units, like (1.25,&nbsp;-2.3).  Such a position
	    doesn't refer to a given pixel, but simply to an
	    immaterial point in the 2D plane.</p>

	  <p>The pixels themselves are indeed <em>square boxes</em> of
	    the 2D plane, whose centers lie in half pixel coordinates.
	    For example, the lower left pixel of a bitmap is delimited
	    by the square (0,&nbsp;0)-(1,&nbsp;1), its center being at
	    location (0.5,&nbsp;0.5).</p>

	  <p>This introduces some differences when computing
	    distances.  For example, the <em>length</em> in pixels of
	    the line [0,&nbsp;0]-[10,&nbsp;0] is&nbsp;11.  However,
	    the vectorial distance between (0,&nbsp;0)-(10,&nbsp;0)
	    covers exactly 10&nbsp;pixel centers, hence its length
	    is&nbsp;10.</p>

	  <p align="center">
	    <img src="grid_1.png"
		 height="390"
		 width="402"
		 alt="bitmap and vector grid">
	  </p>


	  <h3 id="section-2">2. The <code>FT_Bitmap</code> descriptor</h3>

	  <p>In FreeType, a bitmap or pixmap is described through a
	  single structure
	  called <a href="../reference/ft2-basic_types.html#ft_bitmap"><code>FT_Bitmap</code></a>.
	  The fields we are interested in are:</p>

	  <table class="vertical-space">
	    <caption>
	      <b><code>FT_Bitmap</code></b>
	    </caption>

	    <tr>
	      <td valign="top">
		<code>rows</code>
	      </td>
	      <td valign="top">
		the number of rows, i.e., lines, in the bitmap
	      </td>
	    </tr>
	    <tr>
	      <td valign="top">
		<code>width</code>
	      </td>
	      <td valign="top">
		the number of horizontal pixels in the bitmap
	      </td>
	    </tr>
	    <tr>
	      <td valign="top">
		<code>pitch</code>
	      </td>
	      <td valign="top">
		its absolute value is the number of bytes per bitmap
		line; it can be either positive or negative depending
		on the bitmap's vertical orientation
	      </td>
	    </tr>
	    <tr>
	      <td valign="top">
		<code>buffer</code>
	      </td>
	      <td valign="top">
		a typeless pointer to the bitmap pixel buffer
	      </td>
	    </tr>
	    <tr>
	      <td valign="top">
		<code>pixel_mode</code>
	      </td>
	      <td valign="top">
		an enumeration used to describe the pixel format of
		the bitmap; examples
		are <code>ft_pixel_mode_mono</code> for 1-bit
		monochrome bitmaps
		and <code>ft_pixel_mode_grays</code> for 8-bit
		anti-aliased &lsquo;gray&rsquo; values
	      </td>
	    </tr>
	    <tr>
	      <td valign="top">
		<code>num_grays</code>
	      </td>
	      <td valign="top">
		this is only used for &lsquo;gray&rsquo; pixel modes,
		it gives the number of gray levels used to describe
		the anti-aliased gray levels (256&nbsp;by default with
		FreeType&nbsp;2)
	      </td>
	    </tr>
	  </table>

	  <p>Note that the sign of the <code>pitch</code> field
	    determines whether the rows in the pixel buffer are stored
	    in ascending or descending order.</p>

	  <p>Remember that FreeType uses the <em>Y&nbsp;upwards</em>
	    convention in the 2D plane, which means that a coordinate
	    of (0,&nbsp;0) always refer to the <em>lower-left
	    corner</em> of a bitmap.</p>

	  <p>If the pitch is positive, the rows are stored in
	    decreasing vertical position; the first bytes of the pixel
	    buffer are part of the <em>upper</em> bitmap row.</p>

	  <p>On the opposite, if the pitch is negative, the first
	    bytes of the pixel buffer are part of the <em>lower</em>
	    bitmap row.</p>

	  <p>In all cases, one can see the pitch as the byte increment
	    needed to skip to the <em>next lower scanline</em> in a
	    given bitmap buffer.</p>

	  <table class="vertical-space">
	    <tr>
	      <td>
		<img src="up_flow.png"
		     height="261"
		     width="275"
		     alt="negative 'pitch'">
	      </td>
	      <td>
		<img src="down_flow.png"
		     height="263"
		     width="273"
		     alt="positive 'pitch'">
	      </td>
	    </tr>
	  </table>

	  <p>The &lsquo;positive pitch&rsquo; convention is very often
	    used, though some systems might need the other.</p>

          <p>To speed up memory access, <code>pitch</code> is in most
            cases a multiple of 16bit, 32bit, or even 64bit.  It often
            happens that the pitch is thus larger than the necessary
            bits (or bytes) for a bitmap or pixmap row; in such cases,
            unused bits (or bytes) are at the very right (i.e., the
            end) of a row.</p>


	  <h3 id="section-3">3. Converting outlines into bitmaps and
	    pixmaps</h3>

	  <p>Generating a bitmap or pixmap image from a vectorial
	    image is easy with FreeType.  However, one must understand
	    a few points regarding the positioning of the outline in
	    the 2D plane before converting it to a bitmap:</p>

	  <ul>
	    <li>
              <p>The glyph loader and hinter always places the outline
		in the 2D plane so that (0,&nbsp;0) matches its
		character origin.  This means that the glyph's outline
		(and corresponding bounding box), can be placed
		anywhere in the 2D plane (see the graphics in
		section&nbsp;III).</p>
	    </li>
	    <li>
              <p>The target bitmap's area is mapped to the 2D plane,
		with its lower left corner at (0,&nbsp;0).  This means
		that a bitmap or pixmap of dimensions
		[<tt>w</tt>,&nbsp;<tt>h</tt>] will be mapped to a 2D
		rectangle window delimited by
		(0,&nbsp;0)-(<tt>w</tt>,&nbsp;<tt>h</tt>).</p>
	    </li>
	    <li>
              <p>When scan-converting the outline, everything that
		falls within the bitmap window is rendered, the rest
		is ignored.</p>
	    </li>

	    <p>A common mistake made by many developers when they
	      begin using FreeType is believing that a loaded outline
	      can be directly rendered in a bitmap of adequate
	      dimensions.  The following images illustrate why this is
	      a problem.</p>

	    <ul>
              <li>
		The first image shows a loaded outline in the 2D
		plane.
              </li>
              <li>
		The second one shows the target window for a bitmap of
		arbitrary dimensions [<tt>w</tt>,&nbsp;<tt>h</tt>].
              </li>
              <li>
		The third one shows the juxtaposition of the outline
		and window in the 2D plane.
              </li>
              <li>
		The last image shows what will really be rendered in
		the bitmap.
              </li>
	    </ul>

	    <p align="center">
              <img src="clipping.png"
		   height="151"
		   width="539"
		   alt="clipping algorithm">
	    </p>
	  </ul>

	  <p>Indeed, in nearly all cases, the loaded or transformed
	    outline must be translated before it is rendered into a
	    target bitmap, in order to adjust its position relative to
	    the target window.</p>

	  <p>For example, the correct way of creating
	    a <em>stand-alone</em> glyph bitmap is as follows:</p>

	  <ul>
	    <li>
              <p>Get the size of the glyph bitmap.  It can be computed
		directly from the glyph metrics, or by computing its
		bounding box (this is useful when a transformation has
		been applied to the outline after loading it, as the
		glyph metrics are not valid anymore).</p>
	    </li>
	    <li>
              <p>Create the bitmap with the computed dimensions.
		Don't forget to fill the pixel buffer with the
		background color.</p>
	    </li>
	    <li>
              <p>Translate the outline so that its lower left corner
		matches (0,&nbsp;0).  Don't forget that in order to
		preserve hinting, one should use integer, i.e.,
		rounded distances (of course, this isn't required if
		preserving hinting information doesn't matter, like
		with rotated text).  Usually, this means translating
		with a vector
		(<tt>-ROUND(xMin)</tt>,&nbsp;<tt>-ROUND(yMin)</tt>).</p>
	    </li>
	    <li>
              <p>Call the rendering function (it can be
		<a href="../reference/ft2-outline_processing.html#ft_outline_render"><code>FT_Outline_Render</code></a>,
		for example).</p>
	    </li>
	  </ul>

	  <p>In the case where one wants to write glyph images
	    directly into a large bitmap, the outlines must be
	    translated so that their vectorial position corresponds to
	    the current text cursor or character origin.</p>
        </div>

        <!-- ************************************************** -->

        <div class="updated">
          <p>Last update: 13-Feb-2018</p>
        </div>
      </div>
    </div>


    <!-- ************************************************** -->

    <div class="col2">
    </div>
  </div>
</div>


<!-- ************************************************** -->

<div id="TOC">
  <ul>
    <li class="funding">
      <form action="https://www.paypal.com/cgi-bin/webscr"
            method="post"
            target="_top">
        <input type="hidden"
               name="cmd"
               value="_s-xclick">
        <input type="hidden"
               name="hosted_button_id"
               value="SK827YKEALMT4">
        <input type="image"
               src="https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif"
               name="submit"
               alt="PayPal - The safer, easier way to pay online!">
        <img alt=""
             border="0"
             src="https://www.paypalobjects.com/de_DE/i/scr/pixel.gif"
             width="1"
             height="1">
      </form>
    </li>
    <li class="primary">
      <a href="http://freetype.org/index.html">Home</a>
    </li>
    <li class="primary">
      <a href="http://freetype.org/index.html#news">News</a>
    </li>
    <li class="primary">
      <a href="../index.html">Overview</a>
    </li>
    <li class="primary">
      <a href="../documentation.html">Documentation</a>
    </li>
    <li class="primary">
      <a href="http://freetype.org/developer.html">Development</a>
    </li>
    <li class="primary">
      <a href="http://freetype.org/contact.html"
         class="emphasis">Contact</a>
    </li>

    <li>
      &nbsp; <!-- separate primary from secondary entries -->
    </li>

    <li class="secondary">
      <a href="index.html">FreeType Glyph Conventions</a>
    </li>
    <li class="tertiary">
      <a href="glyphs-1.html">Basic Typographic Concepts</a>
    </li>
    <li class="tertiary">
      <a href="glyphs-2.html">Glyph Outlines</a>
    </li>
    <li class="tertiary">
      <a href="glyphs-3.html">Glyph Metrics</a>
    </li>
    <li class="tertiary">
      <a href="glyphs-4.html">Kerning</a>
    </li>
    <li class="tertiary">
      <a href="glyphs-5.html">Text Processing</a>
    </li>
    <li class="tertiary">
      <a href="glyphs-6.html">FreeType Outlines</a>
    </li>
    <li class="tertiary">
      <a href="glyphs-7.html" class="current">FreeType Bitmaps</a>
    </li>
  </ul>
</div>

</div> <!-- id="wrapper" -->

<div id="TOC-bottom">
</div>

</body>
</html>
